package org.giovanolli.mumi.controller.payment;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.controller.UseCaseMode;
import org.giovanolli.mumi.entities.AttentionTime;
import org.giovanolli.mumi.entities.PaymentDetail;
import org.giovanolli.mumi.entities.filter.PatientFilter;
import org.giovanolli.mumi.entities.filter.PaymentConceptFilter;
import org.giovanolli.mumi.entities.filter.PaymentDetailFilter;
import org.giovanolli.mumi.service.PatientService;
import org.giovanolli.mumi.service.PaymentConceptService;
import org.giovanolli.mumi.service.PaymentDetailService;
import org.giovanolli.mumi.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * The Class PaymentDetailController.
 */
@Controller("paymentDetailController")
public class PaymentDetailController extends AbstractController<PaymentDetail> {

	/** The service. */
	@Autowired
	private PaymentDetailService service;

	/** The payment concept service. */
	@Autowired
	private PaymentConceptService paymentConceptService;

	/** The patient service. */
	@Autowired
	private PatientService patientService;

	/** The model. */
	@Autowired
	private PaymentDetailModel model;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#bindEntityToModel()
	 */
	@Override
	protected void bindEntityToModel() {
		PaymentDetail editedPaymentDetail = model.getEditedEntity();
		model.setObservation(editedPaymentDetail.getObservation());
		model.setDate(editedPaymentDetail.getDate());
		model.setAmount(editedPaymentDetail.getAmount());
		model.setPaymentConcept(editedPaymentDetail.getPaymentConcept());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#bindModelToEntity()
	 */
	@Override
	protected void bindModelToEntity() {
		PaymentDetail editedPaymentDetail = model.getEditedEntity();
		editedPaymentDetail.setObservation(model.getObservation());
		editedPaymentDetail.setDate(model.getDate());
		editedPaymentDetail.setAmount(model.getAmount());
		editedPaymentDetail.setPaymentConcept(model.getPaymentConcept());
	}

	/**
	 * Gets the entity class.
	 * 
	 * @return the entity class
	 */
	protected Class<PaymentDetail> getEntityClass() {
		return PaymentDetail.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getModel()
	 */
	@Override
	protected AbstractModel<PaymentDetail> getModel() {
		return model;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#getService()
	 */
	@Override
	protected Service<PaymentDetail> getService() {
		return service;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController#init()
	 */
	public void init() {
		super.init();
		model.setFilter(new PaymentDetailFilter());
		populateNonEntityPropertiesToModel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.controller.AbstractController# populateNonEntityPropertiesToModel()
	 */
	@Override
	public void populateNonEntityPropertiesToModel() {
		model.setSelectablePatients(patientService.getList(new PatientFilter()));
		model.setSelectablePaymentConcepts(paymentConceptService.getList(new PaymentConceptFilter()));
	}

	/**
	 * Prepare entity render.
	 * 
	 * @param uid
	 *            the uid
	 * @param useCaseMode
	 *            the use case mode
	 * @param attentionTime
	 *            the attention time
	 * @throws InstantiationException
	 *             the instantiation exception
	 * @throws IllegalAccessException
	 *             the illegal access exception
	 */
	public void prepareEntityRender(String uid, UseCaseMode useCaseMode, AttentionTime attentionTime) throws InstantiationException, IllegalAccessException {
		// TODO Auto-generated method stub
		super.prepareEntityRender(uid, useCaseMode);

		if (attentionTime != null) {
			model.setPatient(attentionTime.getPatient());
			model.setDate(attentionTime.getDate());
			/* model.setAmount(attentionTime.getFee()); */
		}
	}

}
